<template>
    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0">

            </el-col>
            <el-col :span="12" :xs="24">
                <el-form class="login_form" :model="loginForm" :rules="rules" ref="ruleForm">
                    <h1>Hello</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="loginForm.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input type="password" :prefix-icon="Lock" placeholder="请输入密码" v-model="loginForm.password"
                            show-password></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button :loading="loading" class="login_btn" type="primary" @click="login">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script setup lang="ts">
//引入icon图标
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue';

//引入用户相关的小仓库
import useUserStore from '@/store/modules/user'
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { getCurrentTime } from '@/utils/time';
let useStore = useUserStore()
let router = useRouter()
//获取el-form组件
let ruleForm = ref()

//定义变量控制按钮加载效果
let loading = ref(false)
//收集账号与密码的数据
let loginForm = reactive({
    username: 'admin',
    password: '111111'
})

// Define the login method
const login = async () => {


    //表单校验
    try {
        await ruleForm.value.validate()
    } catch (error) {
        return
    }

    try {

        //登录逻辑
        loading.value = true
        await useStore.userLogin(loginForm)
        //判断登录后的路由路径当中是否有query参数。如果有，就往query参数那里跳转，如果没有，就往首页跳转
        let redirect: string = router.currentRoute.value.query.redirect as string
        router.push(redirect || '/')
        //登录成功跳转到首页
        // window.location.href = '/'
        // router.push('/')
        //登录成功提示信息
        ElMessage.success(getCurrentTime() + ',欢迎回来')
    } catch (error) {
        ElMessage.error((error as Error).message)
    } finally {
        loading.value = false
    }

}
//自定义校验规则
const validatorUserName = (_rule: any, value: any, callback: any) => {
    if (value.length >= 3 && value.length <= 10) {
        callback()
    } else {
        callback(new Error('用户名在3-10位之间'))
    }
}

const validatorPassword = (_rule: any, value: any, callback: any) => {
    if (value.length >= 6 && value.length <= 15) {
        callback()
    } else {
        callback(new Error('密码在6-15位之间'))
    }
}
//定义表单校验需要配置对象
const rules = reactive({
    username: [
        // { required: true, message: '请输入用户名', trigger: 'blur' },
        // { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        { validator: validatorUserName, trigger: 'blur' }
    ],
    password: [
        // { required: true, message: '请输入密码', trigger: 'blur' },
        // { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        { validator: validatorPassword, trigger: 'blur' }
    ]
})

</script>

<style scoped lang="scss">
.login_container {
    width: 100%;
    height: 100vh;
    background: url("@/assets/images/background.jpg") no-repeat;
    background-size: cover;
}

.login_form {
    position: relative;
    width: 80%;
    top: 30%;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
        color: #fff;
        font-size: 40px;
    }

    h2 {
        font-size: 20px;
        color: #fff;
        margin: 20px 0px;
    }

    .login_btn {
        width: 100%;
    }
}
</style>